package test_achieve;

import constant.Constant;
import cucumber.api.java.en.Given;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import test_achieve.courierapp.Oneself;

import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.concurrent.TimeUnit;

/**
 * Created by chenkuo on 2017/6/29.
 */
public class                                                                                                                                                                                                                                                                                                                                                    Devices {

    @Given("^打开浏览器$")
    public void openBrowser() throws InterruptedException, UnknownHostException {
//        LogManager.init();
        String localhost = InetAddress.getLocalHost().getHostAddress();


//        String chenkuoHost = "172.23.1.192";
                String chenkuoHost = "172.23.1.132";
        if (localhost.equals(chenkuoHost)) {
            Constant.driverWeb = new FirefoxDriver();
        } else {
            System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
            Constant.driverWeb = new ChromeDriver();

            zoomChrome();
        }
        Constant.driverWeb.manage().timeouts().implicitlyWait(Constant.hideWaitTime, TimeUnit.SECONDS);

    }

    @Given("^打开火狐浏览器$")
    public void openFirefox() {
        Constant.driverWeb = new FirefoxDriver();
        Constant.driverWeb.manage().timeouts().implicitlyWait(Constant.hideWaitTime, TimeUnit.SECONDS);

    }


    @Given("^进入闪送员APP中$")
    public void notLonginOpenCourierApp() throws InterruptedException {
//        LogManager.init();
        Thread.sleep(2000);
        Constant.driverCourier = getAndroidDriver();//driverCourier是闪送员端
//        Thread.sleep(2000);
//        LogManager.init();
        Constant.driverCourier.manage().timeouts().implicitlyWait(Constant.hideWaitTime, TimeUnit.SECONDS);
    }

    @Given("^a打开用户APP：(.*)$")
    public void a打开用户app() throws Throwable {
        Constant.driverUserapp = Devices.getUserAndroidDriver();
        //启动用户app
        //登录账户->//checkout账户，退出登录 //登录账户
    }


    @Given("^登录状态进入闪送员APP中$")
    public void longinedOpenCourierApp() throws InterruptedException {
//        LogManager.init();
        Constant.driverCourier = getAndroidDriver3();//driverCourier是闪送员端
        Constant.driverCourier.manage().timeouts().implicitlyWait(Constant.hideWaitTime, TimeUnit.SECONDS);
    }


    @Given("^进入闪送员APP中真机测试$")
    public void openAndroid1() {
        Constant.driverCourier = getAndroiddriverCourier();//driverCourier是闪送员端
        Constant.driverCourier.manage().timeouts().implicitlyWait(Constant.hideWaitTime, TimeUnit.SECONDS);
    }

    /**
     * 不安装安装包
     * 识别安卓设备启动
     * 未安装状态下启动
     */
    public static AndroidDriver getAndroidDriver() {  //
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("deviceName", "Android Emulator");
        capabilities.setCapability("platformVersion", "7.0");
        capabilities.setCapability("newCommandTimeout", 800);
//        capabilities.setCapability("app", "C:\\Users\\chenkuo\\Desktop\\cucumber\\cucumber\\src\\test\\resources\\apk\\shansongyuan.apk");
        capabilities.setCapability("appPackage", " com.ishansong");
        capabilities.setCapability("appActivity", ".LoadingActivity");
        capabilities.setCapability("appWaitPackage", "com.ishansong");
        capabilities.setCapability("appWaitActivity", ".activity.LoginActivity");
        capabilities.setCapability(AndroidMobileCapabilityType.UNICODE_KEYBOARD, true);
        capabilities.setCapability(AndroidMobileCapabilityType.RESET_KEYBOARD, true);
        try {
            return new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 用户app启动
     * 安装状态下启动
     * 非登录状态下启动
     */
    public static AndroidDriver getUserAndroidDriver() {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("deviceName", "Android Emulator");
        capabilities.setCapability("platformVersion", "7.0");
        capabilities.setCapability("newCommandTimeout", 800);
        capabilities.setCapability("appPackage", " cn.ishansong");
        capabilities.setCapability("appActivity", ".activity.SplashActivity");
        capabilities.setCapability("appWaitPackage", "cn.ishansong");
        capabilities.setCapability("appWaitActivity", ".activity.SplashActivity");
        capabilities.setCapability(AndroidMobileCapabilityType.UNICODE_KEYBOARD, true);
        capabilities.setCapability(AndroidMobileCapabilityType.RESET_KEYBOARD, true);
        try {
            return new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        Devices.getUserAndroidDriver();
//        Devices.getAndroidDriver();
    }


    /**
     * 安装安装包
     * 识别安卓设备启动
     */

    public static AndroidDriver getAndroiddriverCourier() {  //通过安装包初始化
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("deviceName", "Android Emulator");
        capabilities.setCapability("platformVersion", "7.0");
        capabilities.setCapability("app", "C:\\Users\\chenkuo\\Desktop\\cucumber\\shansong\\src\\main\\java\\apk\\shansongyuan.apk");
        capabilities.setCapability("appPackage", " com.ishansong");
        capabilities.setCapability("appActivity", ".LoadingActivity");
        capabilities.setCapability("appWaitPackage", "com.ishansong");
        capabilities.setCapability("appWaitActivity", ".activity.LoginActivity");
        try {
            return new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return null;
    }


//    public static void main(String[] args) throws Throwable {
//        Constant.driverCourier =getAndroidDriver3();
////        TouchAction action = new TouchAction(Constant.driverCourier);
////        Thread.sleep(4000);
////        Constant.driverCourier.findElement(By.id("local_btn")).click();//点击更新地址位置
////        action.tap(250, 250).perform();//点击空白位置防止一直加载
////        PubTools.waitForPageLoadById(Constant.driverCourier, "custom_title_btn_left");//等待返回按钮出现
////        Constant.driverCourier.findElement(By.id("custom_title_btn_left")).click();//点击返回按钮
//        Oneself oneself = new Oneself();
//        oneself.进入我的闪送页面();
//        oneself.点击查看收支明细();
//
//    }

    /**
     * 不登录启动闪送员App
     */

    public static AndroidDriver getAndroidDriver3() {

        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
        capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 800);
        capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, 7.0);
        capabilities.setCapability(AndroidMobileCapabilityType.NO_SIGN, true);
        capabilities.setCapability(AndroidMobileCapabilityType.UNICODE_KEYBOARD, true);
        capabilities.setCapability(AndroidMobileCapabilityType.RESET_KEYBOARD, true);
        capabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "com.ishansong");
        capabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, "com.ishansong.LoadingActivity");
        capabilities.setCapability(MobileCapabilityType.NO_RESET, true);
        try {
            return new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return null;
    }

    private void zoomChrome() {
        final JavascriptExecutor js = (JavascriptExecutor) Constant.driverWeb;
        js.executeScript("window.open('','testwindow','width=2400,height=2048')");
        Constant.driverWeb.close();
        Constant.driverWeb.switchTo().window("testwindow");
//        js.executeScript("window.moveTo(0,0);");
//        js.executeScript("window.resizeTo(1400,1024);");
    }


}
